extends Panel

const MAX_LEVEL = 7

@onready var refresh_store_button: Button = %RefreshStoreButton
@onready var upgrade_store_button: Button = %UpgradeStoreButton
@onready var quit_store_button: Button = %QuitStoreButton
@onready var store_level: Label = $MarginContainer/VBoxContainer/StoreLevel


@onready var h_box_container_1: HBoxContainer = $MarginContainer/VBoxContainer/HBoxContainer1
@onready var h_box_container_2: HBoxContainer = $MarginContainer/VBoxContainer/HBoxContainer2
@onready var h_box_container_3: HBoxContainer = $MarginContainer/VBoxContainer/HBoxContainer3


var goods_scene: PackedScene = preload("res://ui/package/goods_button.tscn")


var level:int = 1



func _ready() -> void:
	
	upgrade_store_button.pressed.connect(_upgrade_store_button_pressed)
	quit_store_button.pressed.connect(_quit_store_button_pressed)
	refresh_store_button.pressed.connect(_refresh_store_button_pressed)


	
	load_goods()
	
	
	
func load_goods():
	#清空原有點位
	refresh_container(h_box_container_1)
	refresh_container(h_box_container_2)
	refresh_container(h_box_container_3)
	
		#获取18个装备，填充商店
	var goods = EquipManager.get_to_level_equip(level,18)
	for i  in range(goods.size()):
		var item = goods_scene.instantiate() as GoodsButton
		item.item_id = goods[i].character_name
		if i/6 == 0:
			h_box_container_1.add_child(item)
		if i/6 == 1:
			h_box_container_2.add_child(item)
		if i/6 == 2:
			h_box_container_3.add_child(item)
	
	
	
func refresh_container(container:Node):
	var children_button = container.get_children()
	for node in children_button:
		node.queue_free()

func _upgrade_store_button_pressed():
	level = clampi(level+1,1,MAX_LEVEL)
	if level == MAX_LEVEL:
		upgrade_store_button.visible = false
		
	store_level.text = "商店等级:%2d"%[level]
	




func _quit_store_button_pressed():
	visible = false
	
	

func _refresh_store_button_pressed():
	load_goods()
	
	
	
	
	
